<template>
  <div class="knowledge-management">
    <div class="page-header">
      <h1 class="page-title">知识库管理</h1>
      <div class="header-actions">
        <el-button type="success" @click="showImportDialog = true">
          <i class="fas fa-upload mr-2"></i>批量导入
        </el-button>
        <el-button type="primary" @click="showAddDialog = true">
          <i class="fas fa-plus mr-2"></i>添加知识条目
        </el-button>
        <el-button type="primary" @click="refreshData">
          <i class="fas fa-sync-alt mr-2"></i>刷新
        </el-button>
      </div>
    </div>

    <!-- 搜索和筛选 -->
    <div class="search-section">
      <div class="search-row">
        <el-input
          v-model="searchQuery"
          placeholder="搜索知识标题、内容或标签"
          class="search-input"
          clearable
        >
          <template #prefix>
            <i class="fas fa-search"></i>
          </template>
        </el-input>
        <el-select v-model="categoryFilter" placeholder="知识分类" clearable class="category-filter">
          <el-option label="全部" value=""></el-option>
          <el-option label="法律法规" value="law"></el-option>
          <el-option label="司法解释" value="interpretation"></el-option>
          <el-option label="案例解析" value="case-analysis"></el-option>
          <el-option label="实务指南" value="practice-guide"></el-option>
          <el-option label="常见问题" value="faq"></el-option>
        </el-select>
        <el-select v-model="statusFilter" placeholder="状态" clearable class="status-filter">
          <el-option label="全部" value=""></el-option>
          <el-option label="已发布" value="published"></el-option>
          <el-option label="草稿" value="draft"></el-option>
          <el-option label="审核中" value="reviewing"></el-option>
        </el-select>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <!-- 统计信息 -->
    <div class="stats-section">
      <div class="stat-card total">
        <div class="stat-icon">
          <i class="fas fa-brain"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.totalItems }}</div>
          <div class="stat-label">知识条目</div>
        </div>
      </div>
      <div class="stat-card published">
        <div class="stat-icon">
          <i class="fas fa-check-circle"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.publishedItems }}</div>
          <div class="stat-label">已发布</div>
        </div>
      </div>
      <div class="stat-card views">
        <div class="stat-icon">
          <i class="fas fa-eye"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.totalViews }}</div>
          <div class="stat-label">总浏览量</div>
        </div>
      </div>
      <div class="stat-card updated">
        <div class="stat-icon">
          <i class="fas fa-sync"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.recentUpdates }}</div>
          <div class="stat-label">本周更新</div>
        </div>
      </div>
    </div>

    <!-- 知识库内容 -->
    <div class="knowledge-section">
      <div class="section-header">
        <h2 class="section-title">知识库内容</h2>
        <div class="view-controls">
          <el-radio-group v-model="viewMode" size="small">
            <el-radio-button label="table">表格视图</el-radio-button>
            <el-radio-button label="card">卡片视图</el-radio-button>
          </el-radio-group>
        </div>
      </div>

      <!-- 表格视图 -->
      <div v-if="viewMode === 'table'" class="table-view">
        <el-table :data="filteredKnowledge" v-loading="loading" style="width: 100%">
          <el-table-column prop="id" label="ID" width="60"></el-table-column>
          <el-table-column prop="title" label="标题" min-width="250">
            <template #default="{ row }">
              <div class="knowledge-title">
                <span class="title-text">{{ row.title }}</span>
                <el-tag v-if="row.isRecommended" type="warning" size="small">推荐</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="category" label="分类" width="120">
            <template #default="{ row }">
              <el-tag :type="getCategoryType(row.category)">{{ getCategoryText(row.category) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="author" label="作者" width="100"></el-table-column>
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template #default="{ row }">
              <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="views" label="浏览量" width="100" align="center"></el-table-column>
          <el-table-column prop="likes" label="点赞" width="80" align="center">
            <template #default="{ row }">
              <span class="text-red-500">
                <i class="fas fa-heart mr-1"></i>{{ row.likes }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="updatedAt" label="更新时间" width="120"></el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="{ row }">
              <el-button size="small" type="primary" @click="editKnowledge(row)">
                <i class="fas fa-edit mr-1"></i>编辑
              </el-button>
              <el-button size="small" type="info" @click="viewKnowledge(row)">
                <i class="fas fa-eye mr-1"></i>预览
              </el-button>
              <el-button size="small" type="danger" @click="deleteKnowledge(row)">
                <i class="fas fa-trash mr-1"></i>删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 卡片视图 -->
      <div v-else class="card-view">
        <div class="knowledge-grid">
          <div 
            v-for="item in filteredKnowledge" 
            :key="item.id"
            class="knowledge-card"
            :class="{ 'recommended': item.isRecommended }"
          >
            <div class="card-header">
              <div class="card-meta">
                <span class="item-id">#{{ item.id }}</span>
                <el-tag :type="getCategoryType(item.category)" size="small">
                  {{ getCategoryText(item.category) }}
                </el-tag>
              </div>
              <div class="card-status">
                <el-tag :type="getStatusType(item.status)" size="small">
                  {{ getStatusText(item.status) }}
                </el-tag>
                <el-tag v-if="item.isRecommended" type="warning" size="small">推荐</el-tag>
              </div>
            </div>
            
            <div class="card-content">
              <h3 class="knowledge-title">{{ item.title }}</h3>
              <p class="knowledge-summary">{{ item.summary }}</p>
              
              <div class="knowledge-tags">
                <el-tag 
                  v-for="tag in item.tags" 
                  :key="tag" 
                  size="small" 
                  type="info"
                  class="tag-item"
                >
                  {{ tag }}
                </el-tag>
              </div>
            </div>
            
            <div class="card-stats">
              <div class="stat-item">
                <i class="fas fa-eye"></i>
                <span>{{ item.views }}</span>
              </div>
              <div class="stat-item">
                <i class="fas fa-heart"></i>
                <span>{{ item.likes }}</span>
              </div>
              <div class="stat-item">
                <i class="fas fa-user"></i>
                <span>{{ item.author }}</span>
              </div>
              <div class="stat-item">
                <i class="fas fa-calendar"></i>
                <span>{{ item.updatedAt }}</span>
              </div>
            </div>
            
            <div class="card-actions">
              <el-button size="small" type="primary" @click="editKnowledge(item)">
                <i class="fas fa-edit mr-1"></i>编辑
              </el-button>
              <el-button size="small" type="info" @click="viewKnowledge(item)">
                <i class="fas fa-eye mr-1"></i>预览
              </el-button>
              <el-button size="small" type="danger" @click="deleteKnowledge(item)">
                <i class="fas fa-trash mr-1"></i>删除
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-section">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[12, 24, 48, 96]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItems"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 添加知识条目对话框 -->
    <el-dialog v-model="showAddDialog" title="添加知识条目" width="800px">
      <el-form :model="newKnowledge" :rules="knowledgeRules" ref="knowledgeForm" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="newKnowledge.title" placeholder="请输入知识条目标题"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-select v-model="newKnowledge.category" placeholder="选择知识分类">
            <el-option label="法律法规" value="law"></el-option>
            <el-option label="司法解释" value="interpretation"></el-option>
            <el-option label="案例解析" value="case-analysis"></el-option>
            <el-option label="实务指南" value="practice-guide"></el-option>
            <el-option label="常见问题" value="faq"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="摘要" prop="summary">
          <el-input 
            v-model="newKnowledge.summary" 
            type="textarea" 
            rows="3"
            placeholder="请输入知识条目摘要"
          ></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="newKnowledge.tags" placeholder="请输入标签，用逗号分隔"></el-input>
        </el-form-item>
        <el-form-item label="设为推荐">
          <el-switch v-model="newKnowledge.isRecommended"></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddDialog = false">取消</el-button>
          <el-button type="primary" @click="handleAddKnowledge">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'

export default {
  name: 'KnowledgeManagement',
  setup() {
    const loading = ref(false)
    const searchQuery = ref('')
    const categoryFilter = ref('')
    const statusFilter = ref('')
    const viewMode = ref('table')
    const currentPage = ref(1)
    const pageSize = ref(24)
    const showAddDialog = ref(false)
    const showImportDialog = ref(false)
    
    const stats = reactive({
      totalItems: 1856,
      publishedItems: 1623,
      totalViews: 186589,
      recentUpdates: 67
    })
    
    const knowledge = ref([
      {
        id: 1,
        title: '道路交通安全法实施条例详解',
        category: 'law',
        author: '李卫民律师',
        status: 'published',
        views: 8967,
        likes: 456,
        updatedAt: '2024-02-15',
        summary: '详细解读《道路交通安全法实施条例》各项条款，包括交通事故责任认定、处罚标准等...',
        tags: ['交通安全法', '实施条例', '责任认定'],
        isRecommended: true
      },
      {
        id: 2,
        title: '交通事故人身损害赔偿标准',
        category: 'practice-guide',
        author: '张医法律师',
        status: 'published',
        views: 7234,
        likes: 398,
        updatedAt: '2024-02-12',
        summary: '全面介绍交通事故人身损害赔偿的计算标准和方法，包括医疗费、误工费、精神损害抚慰金等...',
        tags: ['人身损害', '赔偿标准', '计算方法'],
        isRecommended: true
      },
      {
        id: 3,
        title: '最新交通事故司法解释汇编',
        category: 'interpretation',
        author: '王志强律师',
        status: 'published',
        views: 5687,
        likes: 287,
        updatedAt: '2024-02-10',
        summary: '收录最高人民法院关于交通事故案件的最新司法解释和指导意见...',
        tags: ['司法解释', '交通事故', '最高法'],
        isRecommended: true
      },
      {
        id: 4,
        title: '机动车保险理赔实务操作指南',
        category: 'practice-guide',
        author: '陈红律师',
        status: 'published',
        views: 6123,
        likes: 341,
        updatedAt: '2024-02-08',
        summary: '详细介绍机动车保险理赔的操作流程、常见争议及处理方法...',
        tags: ['保险理赔', '操作指南', '争议处理'],
        isRecommended: false
      },
      {
        id: 5,
        title: '伤残等级鉴定标准与实务',
        category: 'case-analysis',
        author: '刘建国律师',
        status: 'published',
        views: 4856,
        likes: 256,
        updatedAt: '2024-02-05',
        summary: '详解人体损伤致残程度分级标准，分析典型伤残鉴定案例...',
        tags: ['伤残鉴定', '等级标准', '案例分析'],
        isRecommended: false
      },
      {
        id: 6,
        title: '交通肇事罪量刑标准与辩护要点',
        category: 'law',
        author: '赵美华律师',
        status: 'reviewing',
        views: 2345,
        likes: 128,
        updatedAt: '2024-02-16',
        summary: '分析交通肇事罪的构成要件、量刑标准及有效辩护策略...',
        tags: ['交通肇事罪', '量刑标准', '辩护策略'],
        isRecommended: true
      },
      {
        id: 7,
        title: '非机动车交通事故责任认定争议',
        category: 'faq',
        author: '马律师',
        status: 'draft',
        views: 0,
        likes: 0,
        updatedAt: '2024-02-17',
        summary: '解答非机动车交通事故中常见的责任认定争议问题...',
        tags: ['非机动车', '责任认定', '常见争议'],
        isRecommended: false
      },
      {
        id: 8,
        title: '代驾交通事故法律责任分析',
        category: 'case-analysis',
        author: '孙律师',
        status: 'published',
        views: 3567,
        likes: 189,
        updatedAt: '2024-02-14',
        summary: '分析代驾服务中发生交通事故时各方的法律责任承担...',
        tags: ['代驾', '法律责任', '责任承担'],
        isRecommended: false
      }
    ])
    
    const newKnowledge = reactive({
      title: '',
      category: '',
      summary: '',
      tags: '',
      isRecommended: false
    })
    
    const knowledgeRules = {
      title: [{ required: true, message: '请输入知识条目标题', trigger: 'blur' }],
      category: [{ required: true, message: '请选择知识分类', trigger: 'change' }],
      summary: [{ required: true, message: '请输入知识条目摘要', trigger: 'blur' }]
    }
    
    const filteredKnowledge = computed(() => {
      return knowledge.value.filter(item => {
        const matchesSearch = !searchQuery.value || 
          item.title.includes(searchQuery.value) ||
          item.summary.includes(searchQuery.value) ||
          item.tags.some(tag => tag.includes(searchQuery.value))
        
        const matchesCategory = !categoryFilter.value || item.category === categoryFilter.value
        const matchesStatus = !statusFilter.value || item.status === statusFilter.value
        
        return matchesSearch && matchesCategory && matchesStatus
      })
    })
    
    const totalItems = computed(() => filteredKnowledge.value.length)
    
    const getCategoryType = (category) => {
      const types = {
        'law': 'danger',
        'interpretation': 'warning',
        'case-analysis': 'primary',
        'practice-guide': 'success',
        'faq': 'info'
      }
      return types[category] || 'info'
    }
    
    const getCategoryText = (category) => {
      const texts = {
        'law': '法律法规',
        'interpretation': '司法解释',
        'case-analysis': '案例解析',
        'practice-guide': '实务指南',
        'faq': '常见问题'
      }
      return texts[category] || '未知'
    }
    
    const getStatusType = (status) => {
      const types = {
        'published': 'success',
        'draft': 'info',
        'reviewing': 'warning'
      }
      return types[status] || 'info'
    }
    
    const getStatusText = (status) => {
      const texts = {
        'published': '已发布',
        'draft': '草稿',
        'reviewing': '审核中'
      }
      return texts[status] || '未知'
    }
    
    const handleSearch = () => {
      currentPage.value = 1
    }
    
    const handleSizeChange = (size) => {
      pageSize.value = size
      currentPage.value = 1
    }
    
    const handleCurrentChange = (page) => {
      currentPage.value = page
    }
    
    const refreshData = () => {
      loading.value = true
      setTimeout(() => {
        loading.value = false
        alert('数据已刷新')
      }, 1000)
    }
    
    const editKnowledge = (item) => {
      alert(`编辑知识条目: ${item.title}`)
    }
    
    const viewKnowledge = (item) => {
      alert(`预览知识条目: ${item.title}`)
    }
    
    const deleteKnowledge = (item) => {
      if (confirm(`确定要删除知识条目 "${item.title}" 吗？`)) {
        alert('删除成功')
      }
    }
    
    const handleAddKnowledge = () => {
      alert('添加知识条目功能开发中...')
      showAddDialog.value = false
    }
    
    return {
      loading,
      searchQuery,
      categoryFilter,
      statusFilter,
      viewMode,
      currentPage,
      pageSize,
      showAddDialog,
      showImportDialog,
      stats,
      knowledge,
      newKnowledge,
      knowledgeRules,
      filteredKnowledge,
      totalItems,
      getCategoryType,
      getCategoryText,
      getStatusType,
      getStatusText,
      handleSearch,
      handleSizeChange,
      handleCurrentChange,
      refreshData,
      editKnowledge,
      viewKnowledge,
      deleteKnowledge,
      handleAddKnowledge
    }
  }
}
</script>

<style scoped>
.knowledge-management {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-row {
  display: flex;
  gap: 16px;
  align-items: center;
}

.search-input {
  flex: 1;
  max-width: 400px;
}

.category-filter,
.status-filter {
  width: 120px;
}

.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
  border-left: 4px solid #e5e7eb;
}

.stat-card.total {
  border-left-color: #3b82f6;
}

.stat-card.published {
  border-left-color: #10b981;
}

.stat-card.views {
  border-left-color: #8b5cf6;
}

.stat-card.updated {
  border-left-color: #f59e0b;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6b7280;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #1f2937;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #6b7280;
  margin-top: 4px;
}

.knowledge-section {
  background: white;
  border-radius: 8px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.table-view {
  margin-bottom: 20px;
}

.knowledge-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-text {
  flex: 1;
}

.knowledge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.knowledge-card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 20px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.knowledge-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.knowledge-card.recommended {
  border-left: 4px solid #f59e0b;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.item-id {
  font-size: 12px;
  color: #6b7280;
  font-weight: 500;
}

.card-status {
  display: flex;
  gap: 4px;
}

.card-content {
  margin-bottom: 16px;
}

.knowledge-title {
  font-size: 16px;
  font-weight: bold;
  color: #1f2937;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.knowledge-summary {
  font-size: 14px;
  color: #6b7280;
  margin: 0 0 12px 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.knowledge-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag-item {
  margin: 0;
}

.card-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
  padding: 12px;
  background: white;
  border-radius: 6px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #6b7280;
}

.stat-item i {
  width: 12px;
}

.card-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style> 